﻿using System;
using System.IO;
using System.Reflection;
using System.Threading;
using Microsoft.Win32;
using pub.Interop;

namespace todo
{
    internal class AppRegistrar
    {
        public void CheckRegistered()
        {
            new Thread(_checkRegistered).Start();
        }

        private void _checkRegistered()
        {
            var changed = false;
            changed |= CheckAppRegistered();
            changed |= CheckProgIdRegistered();
            changed |= CheckFileTypeRegistered();
            if (changed)
            {
                Win32.SHChangeNotify(Win32.SHCNE_ASSOCCHANGED,
                        Win32.SHCNF_IDLIST, IntPtr.Zero, IntPtr.Zero);
            }
        }

        private bool CheckFileTypeRegistered()
        {
            var result = false;
            var path = @"Software\Classes";
            var classesKey = Registry.CurrentUser.OpenSubKey(path, true);
            var todoKey = classesKey.CreateSubKey(".todolist");
            var expectedValue = "newzera.todo";
            var defValue = todoKey.GetValue(null);
            if (!expectedValue.Equals(defValue))
            {
                todoKey.SetValue(null, expectedValue);
                result = true;
            }
            todoKey.Close();
            return result;
        }

        private bool CheckProgIdRegistered()
        {
            var result = false;
            var path = @"Software\Classes";
            var classesKey = Registry.CurrentUser.OpenSubKey(path, true);

            path = @"newzera.todo\shell\open\command";
            var openCommandKey = classesKey.CreateSubKey(path);
            var defValue = openCommandKey.GetValue(null);
            var expectedValue = "\"todo.exe\" \"%1\"";
            if (!expectedValue.Equals(defValue))
            {
                openCommandKey.SetValue(null, expectedValue);
                result = true;
            }
            openCommandKey.Close();

            path = @"newzera.todo\DefaultIcon";
            var defIconKey = classesKey.CreateSubKey(path);
            defValue = defIconKey.GetValue(null);
            expectedValue = "\"" + Assembly.GetExecutingAssembly().Location + "\",1";
            if (!expectedValue.Equals(defValue))
            {
                defIconKey.SetValue(null, expectedValue);
            }
            defIconKey.Close();
            classesKey.Close();
            return result;
        }

        private static bool CheckAppRegistered()
        {
            var result = false;
            var path = @"Software\Microsoft\Windows\CurrentVersion\App Paths\";
            var appPathsKey = Registry.CurrentUser.OpenSubKey(path, true);
            var appKey = appPathsKey.CreateSubKey("todo.exe");
            var defValue = appKey.GetValue(null);
            var exeFile = Assembly.GetExecutingAssembly().Location;
            if (!exeFile.Equals(defValue))
            {
                appKey.SetValue(null, exeFile);
                appKey.SetValue("path", Path.GetDirectoryName(exeFile));
                result = true;
            }
            appKey.Close();
            appPathsKey.Close();
            return result;
        }
    }
}